package kit.edu.lego.gruppe3.levels.bridge;

import kit.edu.lego.gruppe3.ComponentsRepository;
import kit.edu.lego.gruppe3.LegoMain;
import kit.edu.lego.gruppe3.actions.Movement;
import kit.edu.lego.gruppe3.actions.Orientation;
import kit.edu.lego.gruppe3.levels.LevelSelector;
import kit.edu.lego.gruppe3.levels.LevelSelector.Level;
import kit.edu.lego.gruppe3.sensors.GroundType;
import kit.edu.lego.gruppe3.sensors.Scanner;
import lejos.robotics.navigation.DifferentialPilot;
import lejos.robotics.subsumption.Behavior;

/**
 * 
 * @author Lac
 * 
 */
public class EndOfBridge implements Behavior {

	private DifferentialPilot pilot=ComponentsRepository.getInstance().getRobotPilot(); 
	@Override
	public void action() {
		Movement.stop();
		BridgeSolver.setLevelCleared(true);
		LevelSelector.getInstance().setCurrentLevel(Level.line);
		Movement.travel(5, 10);
		Scanner.scanAt(0);
		pilot.steer(20);
		while (pilot.isMoving())
			if (Scanner.getGroundTypeAt(0, true) == GroundType.Line)
				pilot.stop();
		
	
		LegoMain.startLine();
	}

	@Override
	public void suppress() {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean takeControl() {
		ComponentsRepository.getInstance();
		return LevelSelector.getInstance().isBridge()
				&& ComponentsRepository.getInstance().getLightSensor()
						.getNormalizedLightValue() > 440;
	}

	private static boolean levelCleared = false;

	public static boolean isLevelCleared() {
		return levelCleared;
	}

	public static void setLevelCleared(boolean cleared) {
		levelCleared = cleared;
	}

}
